﻿using System;
using System.Collections.Specialized;
using System.Web;

namespace ncp.Business.Common
{
   public class CookieHelper
    {
        #region 为Cookie赋值方法
        /**/
        /// <summary>  
        /// 为Cookie赋值  
        /// </summary>  
        /// <param name="cookieName">Cookie名称</param>  
        /// <param name="nvc">Cookie值集合</param>  
        /// <param name="days">Cookie日期  0 为无时间限制。浏览器关闭就结束</param>  
        /// <param name="domain">Cookie站点</param>  
        /// <returns>返回布尔值</returns>  
        public static bool WriteCookie(string cookieName, NameValueCollection nvc, int days, string domain)
        {
            bool boolReturnValue = false;
            if (nvc != null && !string.IsNullOrEmpty(cookieName)) //判断是否能建Cookie  
            {
                var httpCookie = new HttpCookie(cookieName);
                for (int i = 0; i < nvc.Count; i++)
                {
                    httpCookie.Values.Add(nvc.GetKey(i), HttpUtility.UrlEncode(nvc.Get(i))); //将值添入Cookie中  
                }
                if (days > 0)
                {
                    httpCookie.Expires = DateTime.Now.AddDays(days);  //设置Cookie有效期  
                }

                if (!string.IsNullOrEmpty(domain)) //判断Cookie站点有效  
                {
                    httpCookie.Domain = domain; //设置Cookie站点  
                }
                HttpContext.Current.Response.AppendCookie(httpCookie); //添加Cookie  
                boolReturnValue = true;
            }
            return boolReturnValue;
        }
        /// <summary>  
        /// 不限制日期的写cookies
        /// </summary>  
        /// <param name="cookieName">cookie名称</param>  
        /// <param name="nvc">集合</param>  
        /// <returns></returns>  
        public static bool WriteCookieNoDay(string cookieName, NameValueCollection nvc)
        {
            bool boolReturnValue = false;
            if (nvc != null && !string.IsNullOrEmpty(cookieName)) //判断是否能建Cookie  
            {
                var httpCookie = new HttpCookie(cookieName);
                for (int i = 0; i < nvc.Count; i++)
                {
                    httpCookie.Values.Add(nvc.GetKey(i), nvc.Get(i)); //将值添入Cookie中  
                }
                HttpContext.Current.Response.AppendCookie(httpCookie); //添加Cookie  
                boolReturnValue = true;
            }
            return boolReturnValue;
        }
        /**/
        /// <summary>  
        /// 为Cookie赋值  
        /// </summary>  
        /// <param name="cookieName">Cookie名称</param>  
        /// <param name="nvc">Cookie值集合</param>  
        /// <param name="days">Cookie日期</param>  
        /// <returns>返回布尔值</returns>  
        public static bool WriteCookie(string cookieName, NameValueCollection nvc, int days)
        {
            return WriteCookie(cookieName, nvc, days, null);
        }
        /**/
        /// <summary>  
        /// 为Cookie赋值  
        /// </summary>  
        /// <param name="cookieName">Cookie名称</param>  
        /// <param name="nvc">Cookie值集合</param>  
        /// <returns>返回布尔值</returns>  
        public static bool WriteCookie(string cookieName, NameValueCollection nvc)
        {
            return WriteCookie(cookieName, nvc, 0, null);
        }
        #endregion

        #region 更新Cookie
        /**/
        /// <summary>  
        /// 更新Cookie  
        /// </summary>  
        /// <param name="cookieName">Cookie名称</param>  
        /// <param name="nvc">Cookie值集合</param>  
        /// <returns>bool</returns>  
        public static bool UpdateCookie(string cookieName, NameValueCollection nvc)
        {
            bool boolReturnValue = false;
            if (nvc != null && !string.IsNullOrEmpty(cookieName)) //判断是否能建Cookie  
            {
                var httpCookie = new HttpCookie(cookieName);
                var cookie = HttpContext.Current.Request.Cookies[cookieName];
                if (cookie != null)
                {
                    NameValueCollection nonceNvc = cookie.Values; //得到已有的Cookie值集合  
                    {
                        //------------------------循环判断Cookie值是否存在于新Cookie中，如果存在就予以更新-----------------------  
                        for (int i = 0; i < nonceNvc.Count; i++)
                        {
                            string cookieValue = nonceNvc.Get(i);
                            for (int y = 0; y < nvc.Count; y++)
                            {
                                if (nonceNvc.GetKey(i) == nvc.GetKey(y))
                                {
                                    if (cookieValue != null && cookieValue != nvc.Get(y))
                                    {
                                        cookieValue = nvc.Get(y);
                                    }
                                    break;
                                }
                            }
                            if (cookieValue != null)
                                httpCookie.Values.Add(nonceNvc.GetKey(i), cookieValue); //不存在于原Cookie的值 ，被新加入  
                        }
                        //---------------------------------------------------------------------------------------------------------  
                        HttpContext.Current.Response.AppendCookie(httpCookie); //加入Cookie  
                        boolReturnValue = true;
                    }
                }
            }
            return boolReturnValue;
        }
        #endregion

        #region 得到Cookie值集合
        /**/
        /// <summary>  
        /// 得到Cookie值集合  
        /// </summary>  
        /// <param name="cookieName">Cookie名称</param>  
        /// <returns>返回NameValueCollection集合</returns>  
        public static NameValueCollection GetCookie(string cookieName)
        {
            NameValueCollection nvc=null;
            if (!string.IsNullOrEmpty(cookieName) && HttpContext.Current.Request.Cookies[cookieName] != null) //判断Cookie是否存在  
            {
                nvc = new NameValueCollection();
                var httpCookie = HttpContext.Current.Request.Cookies[cookieName];
                if (httpCookie != null)
                    nvc = httpCookie.Values; //得到Cookie值集合  
            }
            return nvc;
        }
        #endregion

        #region 删除Cookie
        /// <summary>  
        /// 删除cookie  
        /// </summary>  
        /// <param name="cookieName">cookie的值</param>  
        /// <param name="cookieDomain">cookie的域</param>  
        /// <returns></returns>  
        public static bool DeleteCookie(string cookieName, string cookieDomain)
        {
            bool boolReturnValue = false;
            HttpCookie httpCookie = HttpContext.Current.Request.Cookies[cookieName];
            if (httpCookie != null)  //如果Cookie存在  
            {
                if (!string.IsNullOrEmpty(cookieDomain))
                {
                    httpCookie.Domain = cookieDomain;
                }
                httpCookie.Expires = DateTime.Now.AddDays(-30); //来他个负30天，看他怎么存在  
                HttpContext.Current.Response.Cookies.Add(httpCookie);
                boolReturnValue = true;
            }
            return boolReturnValue;
        }
        /**/
        /// <summary>  
        /// 删除Cookie  
        /// </summary>  
        /// <param name="cookieName">Cookie名称</param>  
        /// <returns>布尔值</returns>  
        public static bool DeleteCookie(string cookieName)
        {
            return DeleteCookie(cookieName, null);
        }
        #endregion

        #region 得到单独一条Cookie的值
        /**/
        /// <summary>  
        /// 得到单独一条Cookie的值  
        /// </summary>  
        /// <param name="cookieName">Cookie名称</param>  
        /// <param name="keyName">Key名称</param>  
        /// <returns>返回string</returns>  
        public static string GetSingleValue(string cookieName, string keyName)
        {
            string strReturnValue = string.Empty;
            HttpCookie httpCookie = HttpContext.Current.Request.Cookies[cookieName];
            if (httpCookie != null)  //如果Cookie存在  
            {
                var cookie = HttpContext.Current.Request.Cookies[cookieName];
                if (cookie != null)
                    strReturnValue = HttpUtility.UrlDecode(cookie.Values[keyName]); //得到指定Key的Value  
            }
            return strReturnValue;
        }
        /**/
        /// <summary>  
        /// 得到服务器端单独一条Cookie的值  
        /// </summary>  
        /// <param name="cookieName">Cookie名称</param>  
        /// <param name="keyName">Key名称</param>  
        /// <returns>返回string</returns>  
        public static string GetSingleValueFromServer(string cookieName, string keyName)
        {
            string strReturnValue = string.Empty;
            HttpCookie httpCookie = HttpContext.Current.Response.Cookies[cookieName];
            if (httpCookie != null)  //如果Cookie存在  
            {
                var cookie = HttpContext.Current.Response.Cookies[cookieName];
                if (cookie != null)
                    strReturnValue = cookie.Values[keyName]; //得到指定Key的Value  
            }
            return strReturnValue;
        }

        #endregion

        #region 更新单独一条Cookie的值
        /**/
        /// <summary>  
        /// 更新单独一条Cookie的值  
        /// </summary>  
        /// <param name="cookieName">Cookie名称</param>  
        /// <param name="keyName">Key名称</param>  
        /// <param name="value">值</param>  
        /// <returns>返回布尔值</returns>  
        public static bool UpdateSingleValue(string cookieName, string keyName, string value)
        {
            bool boolReturnValue = false;
            var nvc = new NameValueCollection {{keyName, value}};
            if (!string.IsNullOrEmpty(cookieName)) //判断是否能建Cookie  
            {
                HttpCookie httpCookie = HttpContext.Current.Request.Cookies[cookieName];
                if (httpCookie != null)
                {
                    var cookie = HttpContext.Current.Request.Cookies[cookieName];
                    if (cookie != null && cookie.Values[keyName] != null)
                    {
                        boolReturnValue = UpdateCookie(cookieName, nvc);
                    }
                    else
                    {
                        boolReturnValue = WriteCookie(cookieName, nvc);
                    }
                }
                else
                {
                    boolReturnValue = WriteCookie(cookieName, nvc);
                }
            }
            return boolReturnValue;
        }
        #endregion

        #region 添加单独的一条Cookie值
        /// <summary>  
        /// 添加单独的一条Cookie值  
        /// </summary>  
        /// <param name="cookieName">Cookie名称</param>  
        /// <param name="keyName">Key名称</param>  
        /// <param name="value">值</param>  
        /// <returns></returns>  
        public static bool AddSingleCookie(string cookieName, string keyName, string value)
        {
            bool boolReturnValue = false;
            var nvc = new NameValueCollection {{keyName, value}};
            if (!string.IsNullOrEmpty(cookieName)) //判断是否能建Cookie  
            {
                HttpCookie httpCookie = HttpContext.Current.Request.Cookies[cookieName];
                if (httpCookie != null)
                {
                    var cookie = HttpContext.Current.Request.Cookies[cookieName];
                    if (cookie != null && cookie.Values[keyName] != null)
                    {
                        boolReturnValue = UpdateCookie(cookieName, nvc);
                    }
                    else
                    {
                        boolReturnValue = WriteCookie(cookieName, nvc);
                    }
                }
                else
                {
                    boolReturnValue = WriteCookie(cookieName, nvc);
                }
            }
            return boolReturnValue;
        }
        #endregion

        #region 判断是否存在Cookie表
        /// <summary>  
        /// 判断是否存在Cookie表  
        /// </summary>  
        /// <param name="cookieName">Cookie名称</param>  
        /// <returns></returns>  
        public static bool HasCookie(string cookieName)
        {
            bool boolReturnValue = HttpContext.Current.Request.Cookies[cookieName] != null;
            return boolReturnValue;
        }
        #endregion 
    }
}
